package com.fengze.business.service.impl;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.fengze.business.mapper.InquiryRecordMapper;
import com.fengze.business.vo.EarlyStageProjectVO;
import com.fengze.common.annotation.AutoSetInsertValue;
import com.fengze.common.annotation.AutoSetUpdateValue;
import com.fengze.common.config.RuoYiConfig;
import com.fengze.common.utils.DateUtils;
import com.fengze.common.utils.StringUtils;
import com.fengze.common.utils.file.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fengze.business.mapper.EarlyStageProjectMapper;
import com.fengze.business.domain.EarlyStageProject;
import com.fengze.business.service.IEarlyStageProjectService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 项目前期询价项目Service业务层处理
 * 
 * @author lug
 * @date 2024-02-01
 */
@Service
public class EarlyStageProjectServiceImpl implements IEarlyStageProjectService 
{
    @Resource
    private EarlyStageProjectMapper earlyStageProjectMapper;

    @Resource
    private InquiryRecordMapper inquiryRecordMapper;

    Lock lock = new ReentrantLock();

    /**
     * 查询项目前期询价项目
     * 
     * @param id 项目前期询价项目主键
     * @return 项目前期询价项目
     */
    @Override
    public EarlyStageProject selectEarlyStageProjectById(Long id)
    {
        return earlyStageProjectMapper.selectEarlyStageProjectById(id);
    }

    /**
     * 查询项目前期询价项目列表
     * 
     * @param earlyStageProject 项目前期询价项目
     * @return 项目前期询价项目
     */
    @Override
    public List<EarlyStageProject> selectEarlyStageProjectList(EarlyStageProject earlyStageProject)
    {
        return earlyStageProjectMapper.selectEarlyStageProjectList(earlyStageProject);
    }

    /**
     * 新增项目前期询价项目
     * 
     * @param earlyStageProject 项目前期询价项目
     * @return 结果
     */
    @Override
    public int insertEarlyStageProject(EarlyStageProject earlyStageProject)
    {
        earlyStageProject.setCreateTime(DateUtils.getNowDate());
        return earlyStageProjectMapper.insertEarlyStageProject(earlyStageProject);
    }

    /**
     * 修改项目前期询价项目
     * 
     * @param earlyStageProject 项目前期询价项目
     * @return 结果
     */
    @Override
    public int updateEarlyStageProject(EarlyStageProject earlyStageProject)
    {
        earlyStageProject.setUpdateTime(DateUtils.getNowDate());
        return earlyStageProjectMapper.updateEarlyStageProject(earlyStageProject);
    }

    /**
     * 批量删除项目前期询价项目
     * 
     * @param ids 需要删除的项目前期询价项目主键
     * @return 结果
     */
    @Override
    public int deleteEarlyStageProjectByIds(Long[] ids)
    {
        return earlyStageProjectMapper.deleteEarlyStageProjectByIds(ids);
    }

    /**
     * 删除项目前期询价项目信息
     * 
     * @param id 项目前期询价项目主键
     * @return 结果
     */
    @Override
    public int deleteEarlyStageProjectById(Long id)
    {
        return earlyStageProjectMapper.deleteEarlyStageProjectById(id);
    }

    /**
     *
     * @param earlyStageProject
     * @param file
     * @return
     */

    @AutoSetUpdateValue
    @Override
    public int updateEarlyStageProjectWithFile(EarlyStageProject earlyStageProject, MultipartFile file) {
        if (file != null && !file.isEmpty()) {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = null;
            try {
                fileName = FileUploadUtils.upload(filePath, file);
                earlyStageProject.setFileUrl(fileName);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return earlyStageProjectMapper.updateEarlyStageProject(earlyStageProject);
    }

    @AutoSetInsertValue
    @Override
    public int insertEarlyStageProjectWithFile(EarlyStageProject earlyStageProject, MultipartFile file) {
        if (file != null && !file.isEmpty()) {
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = null;
            try {
                fileName = FileUploadUtils.upload(filePath, file);
                earlyStageProject.setFileUrl(fileName);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        lock.lock();
        int insertCount = 0;
        try{
            // 生成询价编号
            // 获取最后一条信息编号
            String lastCode = earlyStageProjectMapper.queryLastCode();
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            if(StringUtils.isEmpty(lastCode)){
                lastCode = "xj" + String.valueOf(year) + "001";
            }else{
                // 年份
                String yearStr = StringUtils.substring(lastCode,2,6);
                if (Integer.valueOf(yearStr) == year) {
                    // 编号
                    String seqStr = StringUtils.substring(lastCode,6);
                    int seqInt = Integer.valueOf(seqStr);
                    ++seqInt;
                    if(seqInt > 999){
                        seqStr = String.valueOf(seqInt);
                    }else{
                        seqStr = StringUtils.leftPad(String.valueOf(seqInt), 3,"0");
                    }
                    lastCode = "xj" + yearStr + seqStr;
                }else{
                    lastCode = "xj" + String.valueOf(year) + "001";
                }
            }
            earlyStageProject.setCode(lastCode);

            insertCount = earlyStageProjectMapper.insertEarlyStageProject(earlyStageProject);
        }finally {
            lock.unlock();
        }
        return insertCount;
    }

    @Override
    public EarlyStageProjectVO selectEarlyStageProjectVOById(Long id) {
        EarlyStageProjectVO earlyStageProjectVO = earlyStageProjectMapper.selectVOById(id);
        int count = inquiryRecordMapper.countByStageProjectId(id);
        earlyStageProjectVO.setInquiryCount(count);

        return earlyStageProjectVO;
    }

    @Override
    public List<EarlyStageProjectVO> selectEarlyStageProjectVOList(EarlyStageProjectVO earlyStageProjectVO) {
        return earlyStageProjectMapper.selectVOList(earlyStageProjectVO);
    }
}
